package com.simon.hse.activity.safe.workpermit;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonAdapter;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 作业许可证申请
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_apply_work_permit_my)
public class WorkPermitApplyActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.tv_apply_work_permit_xgbg)
    TextView mTv_apply_work_permit_xgbg;

    @ViewInject(R.id.ll_danger_stop_bottom)
    LinearLayout mLl_danger_stop_bottom;

    private boolean isShowBottom = false;

    @ViewInject(R.id.tfl_b_gzlx)
    TagFlowLayout mTfl_b_gzlx;

    @ViewInject(R.id.tfl_c_whbs)
    TagFlowLayout mTfl_c_whbs;

    @ViewInject(R.id.tfl_d_aqfh)
    TagFlowLayout mTfl_d_aqfh;

    @ViewInject(R.id.tfl_e_aqcs)
    TagFlowLayout mTfl_e_aqcs;

    @ViewInject(R.id.tfl_f_csqr)
    TagFlowLayout mTfl_f_csqr;

    @ViewInject(R.id.recyclerview_zy)
    RecyclerView mRecyclerview_zy;

    @ViewInject(R.id.recyclerview_tszy)
    RecyclerView mRecyclerview_tszy;

    @ViewInject(R.id.recyclerview_xgf)
    RecyclerView mRecyclerview_xgf;

    @ViewInject(R.id.recyclerview_aqfh)
    RecyclerView mRecyclerview_aqfh;

    private List<String> granfhList = new ArrayList<>();
    CommonAdapter granfhCommonEmptyAdapter;

    @ViewInject(R.id.tv_workarea)
    TextView tv_workarea;

    @ViewInject(R.id.ll_workarea)
    LinearLayout ll_workarea;

    @ViewInject(R.id.tv_workplace)
    EditText tv_workplace;

    @ViewInject(R.id.tv_workunit)
    EditText tv_workunit;

    @ViewInject(R.id.tv_guardian)
    TextView tv_guardian;
    @ViewInject(R.id.ll_guardian)
    LinearLayout ll_guardian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("作业许可证申请");
//        Bundle bundle = getIntent().getExtras();
//        if (bundle == null) {
//            mLl_danger_stop_bottom.setVisibility(View.VISIBLE);
//            mTv_apply_work_permit_xgbg.setText("特殊作业许可证及相关附件信息");
//        } else {
//            mLl_danger_stop_bottom.setVisibility(View.INVISIBLE);
//            mTv_apply_work_permit_xgbg.setText("特殊作业许可证及相关表格");
//        }
        initBGzlx();
        initCWhbs();
        initAqfh();
        initDAqfh();
        initEAqcs();
        initFCsqr();
        initZy();
        initTszy();
        initJXgf();
    }

    @Event(value = {R.id.iv_common_left, R.id.iv_add_specila_work_permit, R.id.iv_add_work_permit, R.id.tv_submission, R.id.tv_save, R.id.tv_cancel
            , R.id.ll_workarea, R.id.ll_guardian})
    private void finish(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.iv_add_work_permit:
                startActivity(new Intent(WorkPermitApplyActivity.this, AddWorkPermitActivity.class));
                break;
            case R.id.iv_add_specila_work_permit:
                Intent intent = new Intent(WorkPermitApplyActivity.this, AddSpecialWorkPermitActivity.class);
                Bundle bundle = new Bundle();
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_submission:
                finish();
                break;
            case R.id.tv_save:
                finish();
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.ll_workarea:
                initSafePop(ll_workarea, tv_workarea, MyApplication.getInstance().mSafeWorkAreaList);
                break;
            case R.id.ll_guardian:
                initSafePop(ll_guardian, tv_guardian, MyApplication.getInstance().mSafeGuardianList);
                break;
        }
    }

    /**
     * B工作类型
     */
    private void initBGzlx() {
        List<String> mList = new ArrayList<>();
        mList.add("承包商工作");
        mList.add("交叉作业");
        mList.add("屏蔽报警");
        mList.add("放射性作业");
        mList.add("非计划性维修工作");
        mList.add("偏离规则/程序要求的工作");
        mList.add("无安全程序可遵循的工作");
        mList.add("解除连锁和安全应急设备，断路作业");
        //预先设置选中
        TagAdapter mTagGzlxAdapter = new TagAdapter<String>(mList) {

            @Override
            public View getView(FlowLayout parent, int position, String bean) {
                View view = LayoutInflater.from(WorkPermitApplyActivity.this).inflate(R.layout.layout_danger_stop_ryfy, null);
                TextView tv_tab_flow_msg = (TextView) view.findViewById(R.id.tv_tab_flow_msg);
                Drawable drawable = getResources().getDrawable(R.drawable.cb_examination_bg);
                // 设置图片的大小
                drawable.setBounds(0, 0, 45, 45);
                // 设置图片的位置，左、上、右、下
                if (!TextUtils.isEmpty(bean)) {
                    tv_tab_flow_msg.setCompoundDrawables(drawable, null, null, null);
                    tv_tab_flow_msg.setCompoundDrawablePadding(10);
                    tv_tab_flow_msg.setText(bean);
                }
                return tv_tab_flow_msg;
            }

            @Override
            public boolean setSelected(int position, String s) {
                /*if (TextUtils.isEmpty(cxbz_remark)) {
                    return false;
                } else {
                    return s.getValue().equals(cxbz_remark);
                }*/
                return false;
            }
        };
//        mTagGzlxAdapter.setSelectedList(1, 3, 7);
        mTfl_b_gzlx.setAdapter(mTagGzlxAdapter);

        mTfl_b_gzlx.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                return true;
            }
        });


        mTfl_b_gzlx.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                // ToastUtils.getInstance().showShortToast(selectPosSet.toString());
            }
        });
    }

    /**
     * C危害辨识
     */
    private void initCWhbs() {
        List<String> mList = new ArrayList<>();
        mList.add("产生火花/静电");
        mList.add("噪音");
        mList.add("旋转设备");
        mList.add("淹没/埋没");
        mList.add("坠落");
        mList.add("腐蚀性液体");
        mList.add("易燃易爆物质");
        mList.add("辐射");
        mList.add("高温/低温");
        mList.add("有毒有害化学品");
        mList.add("触电");
        mList.add("惰性气体");
        mList.add("高压气体/液体");
        mList.add("不利天气");
        mList.add("爆炸性粉尘");
        //预先设置选中
        TagAdapter mTagGzlxAdapter = new TagAdapter<String>(mList) {

            @Override
            public View getView(FlowLayout parent, int position, String bean) {
                View view = LayoutInflater.from(WorkPermitApplyActivity.this).inflate(R.layout.layout_danger_stop_ryfy, null);
                TextView tv_tab_flow_msg = (TextView) view.findViewById(R.id.tv_tab_flow_msg);
                Drawable drawable = getResources().getDrawable(R.drawable.cb_examination_bg);
                // 设置图片的大小
                drawable.setBounds(0, 0, 45, 45);
                // 设置图片的位置，左、上、右、下
                if (!TextUtils.isEmpty(bean)) {
                    tv_tab_flow_msg.setCompoundDrawables(drawable, null, null, null);
                    tv_tab_flow_msg.setCompoundDrawablePadding(10);
                    tv_tab_flow_msg.setText(bean);
                }
                return tv_tab_flow_msg;
            }

            @Override
            public boolean setSelected(int position, String s) {
                /*if (TextUtils.isEmpty(cxbz_remark)) {
                    return false;
                } else {
                    return s.getValue().equals(cxbz_remark);
                }*/
                return false;
            }
        };
//        mTagGzlxAdapter.setSelectedList(1, 3, 7);
        mTfl_c_whbs.setAdapter(mTagGzlxAdapter);

        mTfl_c_whbs.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                return true;
            }
        });


        mTfl_c_whbs.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                // ToastUtils.getInstance().showShortToast(selectPosSet.toString());
            }
        });
    }

    /**
     * D个人安全防护
     */
    private void initDAqfh() {
        List<String> mList = new ArrayList<>();
        mList.add("安全带");
        mList.add("全封闭眼罩");
        mList.add("安全眼镜");
        mList.add("安全帽");
        mList.add("焊接护目镜");
        mList.add("逃生设施");
        mList.add("安全鞋");
        mList.add("防弧面具");
        mList.add("护耳器");
        mList.add("防化服");
        mList.add("防静电服装");
        mList.add("安全绳");
        mList.add("绝缘服");
        mList.add("正压式呼吸器");
        //预先设置选中
        TagAdapter mTagGzlxAdapter = new TagAdapter<String>(mList) {

            @Override
            public View getView(FlowLayout parent, int position, String bean) {
                View view = LayoutInflater.from(WorkPermitApplyActivity.this).inflate(R.layout.layout_danger_stop_ryfy, null);
                TextView tv_tab_flow_msg = (TextView) view.findViewById(R.id.tv_tab_flow_msg);
                Drawable drawable = getResources().getDrawable(R.drawable.cb_examination_bg);
                // 设置图片的大小
                drawable.setBounds(0, 0, 45, 45);
                // 设置图片的位置，左、上、右、下
                if (!TextUtils.isEmpty(bean)) {
                    tv_tab_flow_msg.setCompoundDrawables(drawable, null, null, null);
                    tv_tab_flow_msg.setCompoundDrawablePadding(10);
                    tv_tab_flow_msg.setText(bean);
                }
                return tv_tab_flow_msg;
            }

            @Override
            public boolean setSelected(int position, String s) {
                /*if (TextUtils.isEmpty(cxbz_remark)) {
                    return false;
                } else {
                    return s.getValue().equals(cxbz_remark);
                }*/
                return false;
            }
        };
//        mTagGzlxAdapter.setSelectedList(1, 3, 7);
        mTfl_d_aqfh.setAdapter(mTagGzlxAdapter);

        mTfl_d_aqfh.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                boolean isHave = false;
                for (String temp : granfhList) {
                    if (temp.contains(mList.get(position))) {
                        isHave = true;
                    }
                }
                if (isHave) {
                    granfhList.remove(mList.get(position));
                } else {
                    granfhList.add(mList.get(position));
                }
                granfhCommonEmptyAdapter.notifyDataSetChanged();
                return true;
            }
        });


        mTfl_d_aqfh.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                // ToastUtils.getInstance().showShortToast(selectPosSet.toString());
            }
        });
    }

    /**
     * E工作前安全措施（工作区域准备）
     */
    private void initEAqcs() {
        List<String> mList = new ArrayList<>();
        mList.add("围栏／警戒线");
        mList.add("设路障");
        mList.add("挂工作警示牌");
        mList.add("漏电检测");
        mList.add("火花防护罩");
        mList.add("通风");
        mList.add("通讯工具");
        mList.add("消防设施");
        mList.add("安全冲淋设施");
        mList.add("监护人");
        mList.add("紧急疏散指示");
        mList.add("气体检测仪");
        mList.add("气体检测");
        mList.add("照明");
        mList.add("防爆机具");
        mList.add("急救设施");
        mList.add("设备需隔离");
        mList.add("接地");
        mList.add("夜间照明和警示灯具");
        //预先设置选中
        TagAdapter mTagGzlxAdapter = new TagAdapter<String>(mList) {

            @Override
            public View getView(FlowLayout parent, int position, String bean) {
                View view = LayoutInflater.from(WorkPermitApplyActivity.this).inflate(R.layout.layout_danger_stop_ryfy, null);
                TextView tv_tab_flow_msg = (TextView) view.findViewById(R.id.tv_tab_flow_msg);
                Drawable drawable = getResources().getDrawable(R.drawable.cb_examination_bg);
                // 设置图片的大小
                drawable.setBounds(0, 0, 45, 45);
                // 设置图片的位置，左、上、右、下
                if (!TextUtils.isEmpty(bean)) {
                    tv_tab_flow_msg.setCompoundDrawables(drawable, null, null, null);
                    tv_tab_flow_msg.setCompoundDrawablePadding(10);
                    tv_tab_flow_msg.setText(bean);
                }
                return tv_tab_flow_msg;
            }

            @Override
            public boolean setSelected(int position, String s) {
                /*if (TextUtils.isEmpty(cxbz_remark)) {
                    return false;
                } else {
                    return s.getValue().equals(cxbz_remark);
                }*/
                return false;
            }
        };
//        mTagGzlxAdapter.setSelectedList(1, 3, 7);
        mTfl_e_aqcs.setAdapter(mTagGzlxAdapter);

        mTfl_e_aqcs.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                return true;
            }
        });


        mTfl_e_aqcs.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                // ToastUtils.getInstance().showShortToast(selectPosSet.toString());
            }
        });
    }

    /**
     * F安全措施确认
     */
    private void initFCsqr() {
        List<String> mList = new ArrayList<>();
        mList.add("已切断工艺流程");
        mList.add("设备已隔离");
        mList.add("工作区域准备完成");
        mList.add("已完成上锁、挂签、测试");
        mList.add("人员培训已完成");
        mList.add("个人安全防护装备到位");
        mList.add("工器具安全检查合格并已贴标签");
        mList.add("特殊工种人员均持有有效资质");
        mList.add("安全工作方案审核通过");
        mList.add("办理特殊作业许可证");
        //预先设置选中
        TagAdapter mTagGzlxAdapter = new TagAdapter<String>(mList) {

            @Override
            public View getView(FlowLayout parent, int position, String bean) {
                View view = LayoutInflater.from(WorkPermitApplyActivity.this).inflate(R.layout.layout_danger_stop_ryfy, null);
                TextView tv_tab_flow_msg = (TextView) view.findViewById(R.id.tv_tab_flow_msg);
                Drawable drawable = getResources().getDrawable(R.drawable.cb_examination_bg);
                // 设置图片的大小
                drawable.setBounds(0, 0, 45, 45);
                // 设置图片的位置，左、上、右、下
                if (!TextUtils.isEmpty(bean)) {
                    tv_tab_flow_msg.setCompoundDrawables(drawable, null, null, null);
                    tv_tab_flow_msg.setCompoundDrawablePadding(10);
                    tv_tab_flow_msg.setText(bean);
                }
                return tv_tab_flow_msg;
            }

            @Override
            public boolean setSelected(int position, String s) {
                /*if (TextUtils.isEmpty(cxbz_remark)) {
                    return false;
                } else {
                    return s.getValue().equals(cxbz_remark);
                }*/
                return false;
            }
        };
//        mTagGzlxAdapter.setSelectedList(1, 3, 7);
        mTfl_f_csqr.setAdapter(mTagGzlxAdapter);

        mTfl_f_csqr.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                return true;
            }
        });


        mTfl_f_csqr.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                // ToastUtils.getInstance().showShortToast(selectPosSet.toString());
            }
        });
    }

    /**
     * 作业许可证相关表格
     */
    private void initZy() {
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            mList.add("第" + i + "条数据");
        }

        LinearLayoutManager llm = new LinearLayoutManager(WorkPermitApplyActivity.this);
        mRecyclerview_zy.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerview_zy.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerview_zy.setItemAnimator(new DefaultItemAnimator());
        mRecyclerview_zy.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(WorkPermitApplyActivity.this, R.layout.layout_zyxkz_sq_item, mList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<String>() {
            @Override
            public void setItemData(CommonViewHolder holder, String dataBean) {
                // TextView tv_law_msg = holder.getView(R.id.tv_law_msg);

            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {

            }

            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mCommonEmptyAdapter.setHasStableIds(true);
        mRecyclerview_zy.setAdapter(mCommonEmptyAdapter);
    }

    /**
     * 特殊作业许可证相关表格
     */
    private void initTszy() {
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            mList.add("第" + i + "条数据");
        }

        LinearLayoutManager llm = new LinearLayoutManager(WorkPermitApplyActivity.this);
        mRecyclerview_tszy.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerview_tszy.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerview_tszy.setItemAnimator(new DefaultItemAnimator());
        mRecyclerview_tszy.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(WorkPermitApplyActivity.this, R.layout.layout_zyxkz_sq_item, mList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<String>() {
            @Override
            public void setItemData(CommonViewHolder holder, String dataBean) {
                // TextView tv_law_msg = holder.getView(R.id.tv_law_msg);

            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {

            }

            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mCommonEmptyAdapter.setHasStableIds(true);
        mRecyclerview_tszy.setAdapter(mCommonEmptyAdapter);
    }

    /**
     * J J受影响相关方
     */
    private void initJXgf() {
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            mList.add("第" + i + "条数据");
        }

        LinearLayoutManager llm = new LinearLayoutManager(WorkPermitApplyActivity.this);
        mRecyclerview_xgf.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerview_xgf.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerview_xgf.setItemAnimator(new DefaultItemAnimator());
        mRecyclerview_xgf.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(WorkPermitApplyActivity.this, R.layout.layout_xgf_sq_item, mList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<String>() {
            @Override
            public void setItemData(CommonViewHolder holder, String dataBean) {
                // TextView tv_law_msg = holder.getView(R.id.tv_law_msg);

            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {

            }

            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mCommonEmptyAdapter.setHasStableIds(true);
        mRecyclerview_xgf.setAdapter(mCommonEmptyAdapter);
    }

    /**
     * D个人安全防护详细
     */
    private void initAqfh() {
//        granfhList.add("其他");
        LinearLayoutManager llm = new LinearLayoutManager(WorkPermitApplyActivity.this);
        mRecyclerview_aqfh.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerview_aqfh.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerview_aqfh.setItemAnimator(new DefaultItemAnimator());
        mRecyclerview_aqfh.setNestedScrollingEnabled(false);

        granfhCommonEmptyAdapter = new CommonAdapter(WorkPermitApplyActivity.this, R.layout.layout_work_permit_aqfh_item, granfhList);
        granfhCommonEmptyAdapter.setItemDataListener(new CommonAdapter.ItemDataListener<String>() {
            @Override
            public void setItemData(CommonViewHolder holder, String dataBean) {
                TextView tv_name = holder.getView(R.id.tv_name);
                tv_name.setText(dataBean);
            }
        });
        granfhCommonEmptyAdapter.setHasStableIds(true);
        mRecyclerview_aqfh.setAdapter(granfhCommonEmptyAdapter);
    }
}
